package com.bio.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bio.dao.BaseDAO;

@SuppressWarnings("unchecked")
public class BaseDAOImpl<T> extends HibernateDaoSupport implements BaseDAO<T>{

	protected Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass()
			.getGenericSuperclass()).getActualTypeArguments()[0];

	@Override
	public void delete(Long id) {
		T obj = this.load(id);
		getHibernateTemplate().delete(obj);
	}

	@Override
	public T get(Long id) {
		return (T) getHibernateTemplate().get(entityClass, id);
	}

	@Override
	public Long save(T obj) {
		return (Long) getHibernateTemplate().save(obj);
	}

	@Override
	public void update(T obj) {
		getHibernateTemplate().update(obj);
	}

	@Override
	public void delete(T obj) {
		getHibernateTemplate().delete(obj);
	}

	@Override
	public void merge(T obj) {
		getHibernateTemplate().merge(obj);
	}

	@Override
	public T load(Long id) {
		return (T) getHibernateTemplate().load(entityClass, id);
	}

	public List<T> loadAll() {
		return getHibernateTemplate().loadAll(entityClass);
	}

	
	public List<T> findByCriteria(DetachedCriteria detachedCriteria) {
		return getHibernateTemplate().findByCriteria(detachedCriteria);
	}

	public List<T> findByCriteria(DetachedCriteria detachedCriteria, int first,
			int max) {
		return getHibernateTemplate().findByCriteria(detachedCriteria, first,
				max);
	}

	public Session getHibernateSession() {
		return SessionFactoryUtils.getSession(getHibernateTemplate()
				.getSessionFactory(), false);
	}

	public DetachedCriteria getDetachedCriteria(){
		return DetachedCriteria.forClass(entityClass);
	}

	


}
